{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python38532bit290b3685fea0440087bdc2421e08e2c9",
   "display_name": "Python 3.8.5 32-bit"
  },
  "metadata": {
   "interpreter": {
    "hash": "74a05d7595be28bac99d2127504054f44fced99e19d9e518750e97d0afb0bdac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 插入一个新作文"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "自定义一个HASH"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "hash=input(\"hash:\")"
   ]
  },
  {
   "source": [
    "从下方找到您的作文对应的序号"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0 - 交浅言深.docx\n1 - 善良是朵美丽的花.docx\n2 - 在尝试中成长(1).docx\n3 - 小丑.docx\n4 - 心中有敬.docx\n5 - 最终，我还是看了春晚.docx\n6 - 有你引路，真好-素材二(1).docx\n7 - 有你引路，真好.docx\n8 - 眷.docx\n9 - 箱.docx\n10 - 美丽的智慧.docx\n11 - 这样做值得.docx\n12 - 食.docx\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "docx_File_List=[]\n",
    "for i in os.listdir(os.getcwd()):\n",
    "    if(i[-5:]==\".docx\"):\n",
    "        docx_File_List.append(i)\n",
    "for i in range(0,len(docx_File_List)):\n",
    "    print(str(i)+\" - \"+docx_File_List[i])"
   ]
  },
  {
   "source": [
    "输入作文地址（序号，1个数字）"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "您选择了：【心中有敬.docx】\n"
     ]
    }
   ],
   "source": [
    "index=int(input(\"输入作文地址（序号，1个数字）:\"))\n",
    "print(\"您选择了：【\"+docx_File_List[index]+\"】\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "请确认，【心中有敬】是标题，（Y-标题/N-正文第一行）\n",
      "headline=心中有敬\n",
      "请确认，【郑家宝】是作者栏，（Y-作者栏/N-正文）\n",
      "请确认，【】是题目，（Y-题目/N-正文）\n",
      "请确认，以下是正文，（Y-正文/N-手动操作）\n",
      "\n",
      "\\n\\n翠绿的果实，瘦瘦的，像翡翠雕成的铃铛，但这种绿色是淡淡的，迎着阳光，给人透明的感觉。\\n几个化肥袋平铺在地面上，沾着一些显暗黄色的泥土。几堆不同的水果，摞在化肥袋上面。\\n见我的眼神久久地锁在中间那一堆果实上，坐在对面三轮车上，正用颤巍巍的手点着钱的老人家，操着一口浓郁的方言，开口吆喝道，“土生土长的蒲桃，今天早上才摘下来的，”半晌，又小声的补一句，“那里马上要拆迁了，今年是最后一年了。”\\n心中一震，脑海里又浮现出了爷爷闲来无事时打理的果园，以及果园外面的一排大蒲桃树。\\n毕竟是忙，且不需要依赖果园谋生，那里的蒲桃树始终是任意生长，只在收成的时候才会关注，还总会送一点来我家。这种土生土长的蒲桃，遵守着节令，汁水虽少，嚼起来坚实香脆，别有风味。\\n蒲桃原产地应在江南，为了在江北引种，心中对自然缺乏尊敬的商人们，推出了经过改良的品种。淡红色，肥胖多汁，且一年四季都能吃到，但不管用什么方法吃它，总觉得好像是水做成的，少了蒲桃应该有的气味，尤其是雨季生长的蒲桃，几乎是淡而无味的，而那些记忆中的绿色蒲桃，却一点点在市场中销声匿迹了，就恍如节令从市场中悄悄溜走，躲到一个无人能看见的角落，掩面而泣。\\n后来，果园边缘道路拓宽，那几棵“土种”的蒲桃树首先遭殃。这是舅舅拎着一袋蒲桃到我家时，告诉我的。虽然不在现场，但脑海中却浮起一幅图像：蒲桃树应声倒下，满地青色的蒲桃在阳光下乱滚。说罢，他还一声叹息，什么时候才能再吃到这样的果子呢？\\n以后应该再也吃不到了，卖蒲桃的老太太说道。人不能心贪，要敬重节令，你用了种种的计策，在冬天里，抢先吃了只有夏天才长的菜，夏天到了，怎么办呢?再吃冬天的菜吗?颠了个儿，你费尽心机，不还是整个瞎忙活吗?别心急，慢慢等着吧，一年四季的菜，你都能吃到；还要敬重自然，现在市场上的所有水果无不是经过品种的改良，从前的人是如何也不能想象现在竟有那么大的荔枝、龙眼、枣子的，然而这些新的品种，有时候味道真是不如从前。毕竟，只有野地里，被风吹绿的菜叶，太阳晒红的果子，才是最有味道的。\\n我将老人家的蒲桃全部装到网兜里，付了钱，慢慢地向家走。他的蒲桃虽是露地长的，质量还有推敲的必要。但他的话，浸着一种晚风的爽凉，久久伴着我。阳光斜射在网兜上，那略带柔软的银粉色，被勒割出精致的纹路，好像一副生长的印谱。\\n我知道这样的敬意会随着时间逐渐消失，于是将它们埋入心中。只有内心触动的时候，才会取出，盛入眼前不可见的高脚杯中。\\n仿佛那些遵守着节令的农人们，正坐在八仙桌的对面，携着朝气蓬勃的自然。对饮一口——\\n敬自然，敬节令，敬仍坚守着它们的农人们。\\n\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import time\n",
    "try:\n",
    "    file=open(\"index.json\",\"r\",encoding='utf-8')\n",
    "    file_index_js=json.load(file)\n",
    "    file.close()\n",
    "except IOError:\n",
    "    file_index_js={\"data\":{}}\n",
    "\n",
    "file_detail_json={\"data\":{}}\n",
    "\n",
    "import datetime\n",
    "from docx import Document\n",
    "\n",
    "doc=Document(docx_File_List[index])\n",
    "\n",
    "currentline=0\n",
    "print(\"请确认，【\"+doc.paragraphs[currentline].text+\"】是标题，（Y-标题/N-正文第一行）\")\n",
    "if (input(\"Headline?(Y/N)\")==\"Y\"):\n",
    "    headline=doc.paragraphs[currentline].text\n",
    "    print(\"headline=\"+doc.paragraphs[currentline].text)\n",
    "    currentline=currentline+1\n",
    "print(\"请确认，【\"+doc.paragraphs[currentline].text+\"】是作者栏，（Y-作者栏/N-正文）\")\n",
    "if(input(\"Author?(Y/N)\")==\"Y\"):\n",
    "    currentline=currentline+1\n",
    "\n",
    "QuestionExist=False\n",
    "QuestionContent=\"\"\n",
    "while True:\n",
    "    print(\"请确认，【\"+doc.paragraphs[currentline].text+\"】是题目，（Y-题目/N-正文）\")\n",
    "    if(input(\"Question?(Y/N)\")==\"Y\"):\n",
    "        QuestionExist=True\n",
    "        if(input(\"楷体/宋体?(0/1)\")==\"1\"):\n",
    "            QuestionContent=QuestionContent+\"<p style='font-family:songti'>\"+doc.paragraphs[currentline].text+\"</p>\"\n",
    "        else:\n",
    "            QuestionContent=QuestionContent+\"<p style='font-family:kaiti'>\"+doc.paragraphs[currentline].text+\"</p>\"\n",
    "        currentline=currentline+1\n",
    "    else:\n",
    "        break\n",
    "file_detail_json[\"data\"][\"QuestionExist\"]=QuestionExist\n",
    "if(QuestionExist):\n",
    "    file_detail_json[\"data\"][\"QuestionURL\"]=\"/articlelist/\"+hash+\".zpackage_question\"\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "data__zpackage_article=\"\"\n",
    "for cl in range(currentline,len(doc.paragraphs)):\n",
    "    data__zpackage_article=data__zpackage_article+\"\\\\n\"+doc.paragraphs[cl].text\n",
    "\n",
    "print(\"请确认，以下是正文，（Y-正文/N-手动操作）\\n\")\n",
    "print(data__zpackage_article)\n",
    "if(input(\"请确认，以下是正文，（Y-正文/N-手动操作）：\")==\"Y\"):\n",
    "    pass\n",
    "\n",
    "file_detail_json[\"data\"]={\n",
    "    \"hash\":hash,\n",
    "    \"headline\":headline,\n",
    "    \"body\":\"/articlelist/\"+hash+\".zpackage_article\",\n",
    "    \"datePublishedtime\":\"\",\n",
    "    \"dateModifiedtime\":\"\",\n",
    "    \"originwordexist\":\"/articlelist/\"+docx_File_List[index],\n",
    "    \"handwrittenexist\":False\n",
    "}\n",
    "\n",
    "if(input(\"素材是否虚构（Y/N）：\")==\"Y\"):\n",
    "    file_detail_json[\"data\"][\"fictionmaterial\"]=True\n",
    "else:\n",
    "    file_detail_json[\"data\"][\"fictionmaterial\"]=False\n",
    "\n",
    "if(input(\"创建时间是否为今天（Y/N）：\")==\"Y\"):\n",
    "    dataPandM=datetime.datetime.now().strftime('%Y-%m-%d')\n",
    "else:\n",
    "    dataPandM=input(\"输入日期（yyyy-mm-dd）：\")\n",
    "file_detail_json[\"data\"][\"datePublished\"]=file_detail_json[\"data\"][\"dateModified\"]=dataPandM\n",
    "\n",
    "if(input(\"是否被星标（Y/N）：\")==\"Y\"):\n",
    "    file_detail_json[\"data\"][\"beingStarred\"]=True\n",
    "else:\n",
    "    file_detail_json[\"data\"][\"beingStarred\"]=False\n",
    "if(input(\"是否被否决（Y/N）：\")==\"Y\"):\n",
    "    file_detail_json[\"data\"][\"beingVotedDown\"]=True\n",
    "else:\n",
    "    file_detail_json[\"data\"][\"beingVotedDown\"]=False\n",
    "if(input(\"是否有得分（Y/N）：\")==\"Y\"):\n",
    "    file_detail_json[\"data\"][\"scoreExist\"]=True\n",
    "    file_detail_json[\"data\"][\"getScore\"]=int(input(\"得分（int）：\"))\n",
    "else:\n",
    "    file_detail_json[\"data\"][\"scoreExist\"]=False\n",
    "\n",
    "if(input(\"是否基于其他作文修改（Y/N）：\")==\"Y\"):\n",
    "    file_detail_json[\"data\"][\"ModifiedVersion\"]=True\n",
    "    file_detail_json[\"data\"][\"modifiedOriginVersion\"]=False\n",
    "    try:\n",
    "        file=open(\"index.json\",\"r\",encoding='utf-8')\n",
    "        file_index_js=json.load(file)\n",
    "        file.close()\n",
    "    except IOError:\n",
    "        file_index_js={\"data\":{}}\n",
    "    indexlist=[]\n",
    "    for sb in file_index_js[\"data\"]:\n",
    "        file=open(file_index_js[\"data\"][sb][\"origin\"][13:],\"r\",encoding='utf-8')\n",
    "        file_de_js=json.load(file)\n",
    "        file.close()\n",
    "        print(sb + \"-\" + file_de_js[\"data\"][\"headline\"])\n",
    "    file_detail_json[\"data\"][\"ModifiedFrom\"]=input(\"ModifiedFrom:\")\n",
    "    try:\n",
    "        file=open(\"index.json\",\"r\",encoding='utf-8')\n",
    "        file_index_js=json.load(file)\n",
    "        file.close()\n",
    "    except IOError:\n",
    "        file_index_js={\"data\":{}}\n",
    "    file=open(file_index_js[\"data\"][file_detail_json[\"data\"][\"ModifiedFrom\"]][\"origin\"][13:],\"r\",encoding='utf-8')\n",
    "    file_de_js=json.load(file)\n",
    "    file.close()\n",
    "    file_de_js[\"data\"][\"modifiedOriginVersion\"]=True\n",
    "    file_de_js[\"data\"][\"modifiedVersions\"]=file_de_js[\"data\"][\"modifiedVersions\"] or []\n",
    "    file_de_js[\"data\"][\"modifiedVersions\"].append(file_detail_json[\"data\"][\"hash\"])\n",
    "    file_detail=open(file_index_js[\"data\"][file_detail_json[\"data\"][\"ModifiedFrom\"]][\"origin\"][13:],\"w\",encoding='utf-8')\n",
    "    file_detail_out=json.dumps(file_de_js,sort_keys=True,indent=4,ensure_ascii=False)\n",
    "    file_detail.write(file_detail_out)\n",
    "    file_detail.close()\n",
    "\n",
    "\n",
    "\n",
    "file_index_js[\"data\"][hash]={\"origin\":\"/articlelist/\"+hash+\".json\",\"timestamp\":int(time.time()),\"createtime\":dataPandM}\n",
    "\n"
   ]
  },
  {
   "source": [
    "写文件"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_index=open(\"index.json\",\"w\",encoding='utf-8')\n",
    "file_detail=open(hash+\".json\",\"w\",encoding='utf-8')\n",
    "file_zpackage_article=open(hash+\".zpackage_article\",\"w\",encoding='utf-8')\n",
    "\n",
    "file_index_out=json.dumps(file_index_js,sort_keys=True,indent=4,ensure_ascii=False)\n",
    "file_detail_out=json.dumps(file_detail_json,sort_keys=True,indent=4,ensure_ascii=False)\n",
    "\n",
    "file_index.write(file_index_out)\n",
    "file_detail.write(file_detail_out)\n",
    "file_zpackage_article.write(data__zpackage_article)\n",
    "\n",
    "file_index.close()\n",
    "file_detail.close()\n",
    "file_zpackage_article.close()\n",
    "\n",
    "if(QuestionExist):\n",
    "    file=open(hash+\".zpackage_question\",\"w\",encoding='utf-8')\n",
    "    file.write(QuestionContent)\n",
    "    file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}